﻿using System;
using System.ServiceModel;
using KillBill.Common.View;
using KillBill.Presentation.Shell.AdministrationServiceReference;
using KillBill.Presentation.Shell.IView;
using KillBill.Presentation.Shell.SecurityServiceReference;
using KillBill.Presentation.Common;

namespace KillBill.Presentation.Shell.Presenter
{
    public class LogInPresenter
    {
        private SecurityServiceClient _service;
        private AdministrationServiceClient _administrationService;

        public LogInPresenter(ILogIn view)
        {
            View = view;
            _service = new SecurityServiceClient();
            _administrationService = new AdministrationServiceClient();
        }
        public ILogIn View { get; set; }

        public void Autenticate()
        {
            try
            {
                var session = _service.LogIn(View.Id, View.Password);
                if (session.Success)
                {
                    if (session.Employee != null && session.Employee.Person == null)
                    {
                        Context.Session = null;
                        View.ShowMessage("Iniciar sesión", session.Messages[0], MessageOptions.Ok, MessageIcon.Information);
                    }
                    else
                    {
                        Context.Session = session;
                        View.Close();
                    }
                }
                else
                {
                    View.ShowMessage("Iniciar sesión", session.Messages[0], MessageOptions.Ok, MessageIcon.Error);
                }
            }
            catch (EndpointNotFoundException ex)
            {
                View.ShowConnectionErrorMessage("Iniciar sesión");
            }
            catch (Exception ex)
            {
                View.ShowMessage("Iniciar sesión", ex.Message, MessageOptions.Ok, MessageIcon.Error);
            }
        }
    }
}
